using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MoEngine
{
    public class Scene2DLayer
    {
        public LayerRootGraph Root = new LayerRootGraph();
    }

    public class LayerRootGraph : Graph2D 
    {
    }

    public class Scene2D
    {

        SpriteBatch m_SpriteBatch;

        List<Scene2DLayer> m_Layers = new List<Scene2DLayer>();

        public Scene2D()
        {
            m_SpriteBatch = new SpriteBatch(Core.GlobalDevice);
        }

        public void Attach(Graph2D graph, int layer)
        {
            if (layer >= m_Layers.Count)
            {
                Scene2DLayer[] layers = new Scene2DLayer[layer - m_Layers.Count + 1];
                m_Layers.AddRange(layers);
            }

            if (m_Layers[layer] == null)
                m_Layers[layer] = new Scene2DLayer();

            m_Layers[layer].Root.Attach(graph);
        }

        public virtual void Render(Camera2D camera)
        {
            // determine drawing range
            Point topLeft = camera.TopLeft;

            m_SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f));

            foreach (Scene2DLayer layer in m_Layers)
            {
                layer.Root.RenderAll(camera, m_SpriteBatch);
            }
            m_SpriteBatch.End();
        }

        public virtual void Update(GameTime gameTime)
        {
            foreach (Scene2DLayer layer in m_Layers)
            {
                layer.Root.UpdateAll(gameTime);
            }
        }
    }
}
